package com.zqs.service.impl;

import com.zqs.entity.UsedCar;
import com.zqs.mapper.UsedCarMapper;
import com.zqs.service.UsedCarService;
import com.zqs.service.ex.InsertException;
import com.zqs.service.ex.UpdateException;
import com.zqs.service.ex.UsedCarNothingnessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UsedCarServiceImpl implements UsedCarService {

    @Autowired
    private UsedCarMapper usedCarMapper;

    @Override
    public void add(UsedCar usedCar) {
        usedCar.setState(1);
        Integer row = usedCarMapper.addUsedCar(usedCar);
        if (row != 1){
            throw new InsertException("插入时发生未知异常!");
        }
    }

    @Override
    public List<UsedCar> getByUid(Integer uid) {
        List<UsedCar> usedCarList = usedCarMapper.getByUid(uid);
        if (usedCarList == null){
            throw new UsedCarNothingnessException("二手车未找到!");
        }
        return usedCarList;
    }

    @Override
    public List<UsedCar> getAll() {
        List<UsedCar> usedCarList = usedCarMapper.getAll();
        if (usedCarList == null){
            throw new UsedCarNothingnessException("二手车未找到!");
        }
        return usedCarList;
    }

    @Override
    public void updateStateById(Integer id,Integer uid) {
        Integer integer = usedCarMapper.updateStateById(id,uid);
        if (integer != 1){
            throw new UpdateException("修改时发生未知异常!");
        }
    }
}
